package com.info3k.client.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.Info3KService;
import com.info3k.client.Info3KServiceAsync;
import com.info3k.client.components.recaptcha.RecaptchaWidget;
import com.info3k.client.components.validators.PasswordTextBoxValidator;
import com.info3k.client.components.validators.TextBoxValidator;
import com.info3k.client.domain.User;
import com.info3k.client.enums.TabTypes;
import com.info3k.client.enums.UserTypes;
import com.info3k.client.utils.ToolsClient;

public class RegisterManager extends GlassDialogBox{

	private VerticalPanel dialogVPanel = new VerticalPanel();
	private Button registerButton = new Button(Info3K.msgs.register());
	
	private TextBoxValidator username = new TextBoxValidator();
	private TextBoxValidator emailAddress = new TextBoxValidator();
	private PasswordTextBoxValidator password = new PasswordTextBoxValidator();
	private RecaptchaWidget recaptcha = new RecaptchaWidget("6Ld_8AkAAAAAACa_FB4gLUXuae28YVOHTucoBFry");
	
	private Info3K info3k;
	
 	private Info3KServiceAsync infoService = GWT.create(Info3KService.class);
	
	public RegisterManager(Info3K info){
		this.info3k = info;
		setText(Info3K.msgs.register());
		setAnimationEnabled(true);
		final Button closeButton = new Button(Info3K.msgs.close());
		closeButton.getElement().setId("closeButton");
		registerButton.getElement().setId("registerButton");

		final Label lblUsername = new Label(Info3K.msgs.username() + ": ");
		final Label lblPassword = new Label(Info3K.msgs.password() + ": ");
		final Label lblEmailAddress = new Label(Info3K.msgs.email() + ": ");
		final HTML errorMessage = new HTML();
		errorMessage.getElement().getStyle().setProperty("color", "red");
		errorMessage.setWidth("140px");
		
		username.getElement().getStyle().setProperty("width", "95%");
		emailAddress.getElement().getStyle().setProperty("width", "95%");
		password.getElement().getStyle().setProperty("width", "95%");
		
		username.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				infoService.isAvailableUsername(username.getText(), new AsyncCallback<Boolean>(){
					public void onFailure(Throwable caught) {
						InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
					}

					public void onSuccess(Boolean result) {
						if(!result) {
							errorMessage.setHTML(Info3K.msgs.userAlreadyExist(username.getText()));
							username.setValid(false);
						} else {
							username.setValid(true);
							errorMessage.setHTML("");
						}
					}
				});
			}
		});
		
		password.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				if(password.getText().length()<6){
					errorMessage.setHTML(Info3K.msgs.passwordNotValid());
					password.setValid(false);
				} else {
					password.setValid(true);
					errorMessage.setHTML("");
				}
			}
		});
		
		emailAddress.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				if(!ToolsClient.isEmail(emailAddress.getText())){
					errorMessage.setHTML(Info3K.msgs.emailAddressNotValid());
					emailAddress.setValid(false);
				} else {
					emailAddress.setValid(true);
					errorMessage.setHTML("");
				}
			}
		});
		dialogVPanel.add(lblUsername);
		dialogVPanel.add(username);
		dialogVPanel.add(lblPassword);
		dialogVPanel.add(password);
		dialogVPanel.add(lblEmailAddress);
		dialogVPanel.add(emailAddress);
		dialogVPanel.add(errorMessage);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(recaptcha);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(registerButton);
		horizontalPanel.add(closeButton);
		
		dialogVPanel.add(horizontalPanel);
		setWidget(dialogVPanel);
		setModal(true);
		closeButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				hide();
			}
		});	
		
		registerButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(username.isValid() && password.isValid() && emailAddress.isValid()){
					if(recaptcha.getResponse()!=null && !(recaptcha.getResponse().length()>0)) {
						InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.invalidCaptcha());
						recaptcha.reload();
						return;
					}
					Info3K.loader.setVisible(true);
					User u = new User();
					u.setUsername(username.getText());
					u.setPassword(password.getText());
					u.setEmail(emailAddress.getText());
					u.setUserType(UserTypes.USER);
					infoService.registerUser(u, recaptcha.getResponse(), recaptcha.getChallenge(), new AsyncCallback<Boolean>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Boolean result) {
							Info3K.loader.setVisible(false);
							if(result){
								info3k.getTabManager().refresh(TabTypes.LATEST_USERS_LEFT);
								info3k.getTabManager().refresh(TabTypes.LATEST_USERS_TOP);
								hide();
								InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.registerSuccessfully(username.getText()));
							} else {
								InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.invalidCaptcha());
								recaptcha.reload();
							}
						}
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
	}
}
